package com.squirrel.common.controller;

import com.squirrel.common.annotation.AuthToken;
import com.squirrel.common.param.video.MultipartFileParam;
import com.squirrel.common.param.video.PageParam;
import com.squirrel.common.param.video.VideoParam;
import com.squirrel.common.response.ResponseResult;
import com.squirrel.common.response.ResponseResultUtils;
import com.squirrel.common.response.code.CodeEnum;
import com.squirrel.common.response.page.PageResult;
import com.squirrel.common.service.VideoService;
import com.squirrel.common.vo.video.VideoInfoVO;
import com.squirrel.common.vo.video.VideoUpVO;
import com.squirrel.common.vo.video.VideoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * VideoController 视频控制器
 * </p>
 *
 * @author 关远键
 * @since 2021/11/11 16:06
 */
@RestController
@RequestMapping("/api/video")
@Api(tags = "视频控制器")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @GetMapping("/getRandomVideo/{size}")
    @ApiOperation("随机视频")
    public ResponseResult<PageResult<VideoVO>> getRandomVideo(@PathVariable("size") int size) {
        return ResponseResultUtils.success(videoService.getRandomVideo(size));
    }

    @GetMapping("/getByChannel")
    @ApiOperation("指定频道视频")
    @ApiImplicitParam(name = "channelId", value = "频道编号", dataTypeClass = Integer.class, required = true)
    public ResponseResult<PageResult<VideoVO>> getByChannel(PageParam pageParam, Integer channelId) {
        return ResponseResultUtils.success(videoService.getByChannel(pageParam, channelId));
    }

    @GetMapping("/getByKey")
    @ApiOperation("搜索视频（key可以是用户昵称或视频标题）")
    @ApiImplicitParam(name = "key", value = "关键字", dataTypeClass = String.class, required = true)
    public ResponseResult<PageResult<VideoVO>> getByKey(PageParam pageParam, String key) {
        return ResponseResultUtils.success(videoService.getByKey(pageParam, key));
    }

    @GetMapping("/getByUserId/{userId}")
    @ApiOperation("视频（up主视角）")
    public ResponseResult<List<VideoUpVO>> getByChannel(@PathVariable("userId") Integer userId) {
        return ResponseResultUtils.success(videoService.getByUserId(userId));
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("播放视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "视频ID", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataTypeClass = Integer.class, required = false)
    })
    public ResponseResult<VideoInfoVO> getById(@PathVariable Integer id, Integer userId) {
        return ResponseResultUtils.success(videoService.getById(userId, id));
    }

    @PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation("上传视频（分片taskId必须一致）")
    @AuthToken
    public ResponseResult<String> upload(MultipartFileParam fileParam) {
        return ResponseResultUtils.success(CodeEnum.SUCCESS, videoService.upload(fileParam));
    }

    @PostMapping(value = "/uploadSuccessCallback")
    @ApiOperation("上传视频成功回调（文件名必须一致）")
    @AuthToken
    public ResponseResult<Integer> uploadSuccessCallback(VideoParam videoParam) {
        return ResponseResultUtils.success(videoService.uploadSuccessCallback(videoParam));
    }



//    @Autowired
//    private NonStaticResourceHttpRequestHandler nonStaticResourceHttpRequestHandler;
//
//    public static final String PATH = "";
//
//    @GetMapping("/getResource1/{videoId}")
//    @ApiOperation("视频流1")
//    public void getPlayResource(@PathVariable("videoId") Long videoId,
//                                HttpServletRequest request,
//                                HttpServletResponse response){
//
//        Video video = videoService.getById(videoId);
//        if (video != null) {
//            Path path = Paths.get(PATH + video.getVideoAddress());
//            System.out.println(path);
//            if (Files.exists(path)) {
//                try {
//                    String mimeType = Files.probeContentType(path);
//                    if (StringUtils.hasText(mimeType)) {
//                        response.setContentType(mimeType);
//                    }
//                    request.setAttribute(NonStaticResourceHttpRequestHandler.ATTR_FILE, path);
//                    nonStaticResourceHttpRequestHandler.handleRequest(request, response);
//                } catch (Exception e) {
//                    System.out.println(e.getMessage());
//                }
//            } else {
//                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
//                response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
//            }
//        }
//    }
//
//    @GetMapping("/getResource2/{videoId}")
//    @ApiOperation("视频流2")
//    public FileSystemResource getPreview3(@PathVariable("videoId") Integer videoId,
//                                                        HttpServletResponse response) {
//        String path = PATH + videoService.getById(videoId).getAddress();
//        System.out.println(path);
//        return new FileSystemResource(path);
//    }


}
